<?php

/**
 * Faett_Package
 *
 * NOTICE OF LICENSE
 * 
 * Faett_Package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Faett_Package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Faett_Package.  If not, see <http://www.gnu.org/licenses/>.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Faett_Package to newer
 * versions in the future. If you wish to customize Faett_Package for your
 * needs please refer to http://www.faett.net for more information.
 *
 * @category   Faett
 * @package    Faett_Package
 * @copyright  Copyright (c) 2009 <tw@faett.net> Tim Wagner
 * @license    <http://www.gnu.org/licenses/> 
 * 			   GNU General Public License (GPL 3)
 */

?>
<?php

/**
 * @see Faett_Package_Block_Adminhtml_Catalog_Product_Edit_Tab_Package_Links
 */
?>
<?php $_product = $this->getProduct()?>
<div class="fieldset">
<table cellspacing="0" class="form-list">

    <tbody>
        <tr class="headings">
            <td class="label"><label for="name"><?php echo Mage::helper('package')->__('Title')?></label>
            </td>
            <td class="value">
                <input type="text" class="input-text" name="product[links_title]" value="<?php echo $_product->getId()?$_product->getLinksTitle():$this->getLinksTitle() ?>" <?php echo ($_product->getStoreId() && $this->getUsedDefault())?'disabled="disabled"':'' ?> />
            </td>
            <td class="scope-label"><?php if (!Mage::app()->isSingleStoreMode()): ?>[STORE VIEW]<?php endif; ?></td>
            <td class="value use-default">
            <?php if($_product->getStoreId()): ?>
                <input id="link_title_default" type="checkbox" name="use_default[]" value="links_title" onclick="toggleValueElements(this, this.parentNode.parentNode)" <?php echo $this->getUsedDefault()?'checked="checked"':'' ?> />
                <label class="normal" for="link_title_default">Use Default Value</label>
            <?php endif; ?>
            </td>
        </tr>
    </tbody>
</table>
<br />
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" class="data border">
    <col width="45%" />
    <col />
    <col />
    <col width="1" />
    <thead>
        <tr class="headings">
            <th><?php echo Mage::helper('package')->__('Title')?> <span class="required">*</span></th>
            <th><?php echo Mage::helper('package')->__('Sample')?></th>
            <th><?php echo Mage::helper('package')->__('File')?></th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="8" class="a-right"><?php echo $this->getAddButtonHtml()?>&nbsp;<?php echo $this->getUploadButtonHtml() ?></td>
        </tr>
    </tfoot>
    <tbody id="link_items_body">
    </tbody>
</table>
</div>
</div>
</div>

<script type="text/javascript">
//<![CDATA[
var linkTemplate = '<tr>'+
    '<td>'+
        '<input type="hidden" class="__delete__" name="package[link][{{id}}][is_delete]" value="" />'+
        '<input type="hidden" name="package[link][{{id}}][link_id]" value="{{link_id}}" />'+
        '<input type="text" class="required-entry input-text" name="package[link][{{id}}][title]" value="{{title}}" />'+
        '<?php echo $_product->getStoreId()?'<input type="checkbox" id="package_link_{{id}}_title" name="package[link][{{id}}][use_default_title]" value="1" /><label class="normal" for="package_link_{{id}}_title">Use Default Value</label>':'' ?>'+
    '</td>'+
    '<td>'+
        '<div class="files-wide">'+
            '<div class="row">'+
                '<label for="package_link_{{id}}_sample_file_type"><input type="radio" class="radio" id="package_link_{{id}}_sample_file_type" name="package[link][{{id}}][sample][type]" value="file"{{sample_file_checked}} /> File:</label>'+
                '<input type="hidden" id="package_link_{{id}}_sample_file_save" name="package[link][{{id}}][sample][file]" value="{{sample_file_save}}" />'+
                '<div id="package_link_{{id}}_sample_file" class="uploader">'+
                    '<div id="package_link_{{id}}_sample_file-old" class="file-row-info"></div>'+
                    '<div id="package_link_{{id}}_sample_file-new" class="file-row-info"></div>'+
                    '<div class="buttons">'+
                        '<div id="package_link_{{id}}_sample_file-install-flash" style="display:none">'+
                            '<?php echo Mage::helper('media')->__('This content requires last version of Adobe Flash Player. <a href="%s">Get Flash</a>', 'http://www.adobe.com/go/getflash/') ?>'+
                        '</div>'+
                    '</div>'+
                    '<div class="clear"></div>'+
                '</div>'+
            '</div>'+
            '<div class="row">'+
                '<label for="package_link_{{id}}_sample_url_type"><input type="radio" class="radio" id="package_link_{{id}}_sample_url_type" name="package[link][{{id}}][sample][type]" value="url"{{sample_url_checked}} /> URL:</label><input type="text" class="input-text" name="package[link][{{id}}][sample][url]" value="{{sample_url}}" />'+
            '</div>'+
            '<div>'+
                '<span id="package_link_{{id}}_sample_container"></span>'+
            '</div>'+
        '</div>'+
    '</td>'+
    '<td>'+
        '<div class="files-wide">'+
            '<div class="row">'+
                '<label for="package_link_{{id}}_file_type"><input type="radio" class="radio validate-one-required-by-name" id="package_link_{{id}}_file_type" name="package[link][{{id}}][type]" value="file"{{file_checked}} /> File:</label>'+
            '<input type="hidden" class="validate-package-file" id="package_link_{{id}}_file_save" name="package[link][{{id}}][file]" value="{{file_save}}" />'+
                '<div id="package_link_{{id}}_file" class="uploader">'+
                    '<div id="package_link_{{id}}_file-old" class="file-row-info"></div>'+
                    '<div id="package_link_{{id}}_file-new" class="file-row-info new-file"></div>'+
                    '<div class="buttons">'+
                        '<div id="package_link_{{id}}_file-install-flash" style="display:none">'+
                            '<?php echo Mage::helper('media')->__('This content requires last version of Adobe Flash Player. <a href="%s">Get Flash</a>', 'http://www.adobe.com/go/getflash/') ?>'+
                        '</div>'+
                    '</div>'+
                    '<div class="clear"></div>'+
                '</div>'+
            '</div>'+
            '<div class="row">'+
                '<label for="package_link_{{id}}_url_type"><input type="radio" class="radio validate-one-required-by-name" id="package_link_{{id}}_url_type" name="package[link][{{id}}][type]" value="url"{{url_checked}} /> URL:</label><input type="text" class="validate-package-url input-text" name="package[link][{{id}}][link_url]" value="{{link_url}}" />'+
            '</div>'+
            '<div>'+
                '<span id="package_link_{{id}}_link_container"></span>'+
            '</div>'+
        '</div>'+
    '</td>'+
    '<td>'+
        '<button type="button" class="scalable delete icon-btn delete-link-item"><span>Delete</span></button>'+
    '</td>'+
'</tr>';

var linkItems = {
    tbody : $('link_items_body'),
    templateSyntax : /(^|.|\r|\n)({{(\w+)}})/,
    templateText : linkTemplate,
    itemCount : 0,
    add : function(data) {
        alertAlreadyDisplayed = false;
        this.template = new Template(this.templateText, this.templateSyntax);

        if(!data.link_id){
            data = {};
            data.link_id  = 0;
            data.link_type = 'file';
            data.sample_type = 'none';
        }

        data.id = this.itemCount;

        if (data.link_type == 'url') {
            data.url_checked = ' checked="checked"';
        } else if (data.link_type == 'file') {
            data.file_checked = ' checked="checked"';
        }
        if (data.sample_type == 'url') {
            data.sample_url_checked = ' checked="checked"';
        } else if (data.sample_type == 'file') {
            data.sample_file_checked = ' checked="checked"';
        }

        Element.insert(this.tbody, {'bottom':this.template.evaluate(data)});

        scopeTitle = $('package_link_'+data.id+'_title');
        if (scopeTitle) {
            Event.observe(scopeTitle, 'click', function(event){
                scopeElm = $(Event.findElement(event, 'input'));
                titleField = scopeElm.up(0).down('input[type="text"]');
                if (scopeElm.checked == true) {
                    titleField.disabled = true;
                } else {
                    titleField.disabled = false;
                }
            });
        }
        if (!data.store_title && scopeTitle) {
            scopeTitle.up(0).down('input[type="text"]').disabled = true;
            scopeTitle.checked = true;
        }

        sampleUrl = $('package_link_'+data.id+'_sample_url_type');
        linkUrl = $('package_link_'+data.id+'_url_type');

        if (!data.file_save) {
            data.file_save = [];
        }
        if (!data.sample_file_save) {
            data.sample_file_save = [];
        }

        // link sample file
        new Package.FileUploader(
            'linkssample',
            'linkssample_'+data.id,
            sampleUrl.up('td'),
            'package[link]['+data.id+'][sample]',
            data.sample_file_save,
            'package_link_'+data.id+'_sample_file',
            <?php echo $this->getConfigJson('link_samples') ?>
        );
        // link file
        new Package.FileUploader(
            'links',
            'links_'+data.id,
            linkUrl.up('td'),
            'package[link]['+data.id+']',
            data.file_save,
            'package_link_'+data.id+'_file',
            <?php echo $this->getConfigJson() ?>
        );

        linkFile = $('package_link_'+data.id+'_file_type');
        linkFile.advaiceContainer = 'package_link_'+data.id+'_link_container';
        linkUrl.advaiceContainer = 'package_link_'+data.id+'_link_container';
        $('package_link_'+data.id+'_file_save').advaiceContainer = 'package_link_'+data.id+'_link_container';

        sampleFile = $('package_link_'+data.id+'_sample_file_type');

        this.itemCount++;
        this.bindRemoveButtons();
    },
    remove : function(event){
        var element = $(Event.findElement(event, 'tr'));
        alertAlreadyDisplayed = false;
        if(element){
            element.down('input[type="hidden"].__delete__').value = '1';
            Element.select(element, 'div.flex').each(function(elm){
                elm.remove();
            });
            element.addClassName('no-display');
            element.addClassName('ignore-validate');
            element.hide();
        }
    },
    bindRemoveButtons : function(){
        var buttons = $$('tbody#link_items_body .delete-link-item');
        for(var i=0;i<buttons.length;i++){
            if(!$(buttons[i]).binded){
                $(buttons[i]).binded = true;
                Event.observe(buttons[i], 'click', this.remove.bind(this));
            }
        }
    }
}

linkItems.bindRemoveButtons();

if ($('package_link_purchase_type')) {
    Event.observe('package_link_purchase_type', 'change', linkItems.togglePriceFields.bind());
}

if($('add_link_item')) {
    Event.observe('add_link_item', 'click', linkItems.add.bind(linkItems));
}

<?php foreach ($this->getLinkData() as $item): ?>
    linkItems.add(<?php echo $item->toJson()?>);
<?php endforeach; ?>

Validation.addAllThese([
    ['validate-package-link-sample-file', 'Please specify File.', function(v,element) {
            fileSaveElm = element.up('div').next('input[type="hidden"]');
            if (element.checked && (fileSaveElm.value == '' || fileSaveElm.value == '[]')) {
                return false;
            }
            return true;
        }]
    ]);
Validation.addAllThese([
    ['validate-package-link-sample-url', 'Please specify Sample URL.', function(v,element) {
            if (element.checked && element.up('p').down('input[type="text"]').value == '') {
                return false;
            }
            return true;
        }]
    ]);
//]]>
</script>